package lcr;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

public class Test0046 {
}

class Solution0046_1 {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ans = new ArrayList<>();
        ArrayDeque<TreeNode> deque = new ArrayDeque<>();
        if (root == null) return ans;

        ans.add(root.val);
        deque.add(root);
        while (!deque.isEmpty()) {
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.poll();
                if (node.left != null) deque.add(node.left);
                if (node.right != null) deque.add(node.right);
            }
            if (!deque.isEmpty()) ans.add(deque.getLast().val);
        }
        return ans;
    }
}
